﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using LTP.Common;

namespace CJJCH.Web.Manage
{
    public partial class PicNewsEdit : PowerBase
    {
        CJJCH.BLL.T_NET_PicNews bll_pic = new BLL.T_NET_PicNews();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitRequest();
                ShowPicNews();
            }
            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSave, "checkform()", false);
        }

        private void InitRequest()
        {
            if (Request.QueryString["ID"] != null && LTP.Common.CheckInput.CheckGUID(Request.QueryString["ID"].ToString()))
            {
                ViewState["ID"] = Request.QueryString["ID"].ToString();
            }
            else
            {
                Response.Redirect("PicNewsList.aspx");
            }
        }
        private void ShowPicNews()
        {
            CJJCH.Model.T_NET_PicNews model = bll_pic.GetPicNewsModel(new Guid(ViewState["ID"].ToString()));
            if (model != null)
            {
                this.txtHrefUrl.Text = model.HrefUrl;
                this.lblImg.Text = model.PicUrl;
                this.chkIsShow.Checked = model.IsShow;
                this.txtOrder.Text = model.OrderNum.ToString();
                this.txtInstructions.Text = model.Instructions;
            }
            else
            {
                LTP.Common.WindowClass.WindowGoHref("操作错误！", "PicNewsList.aspx");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string error = CheckInput();
            if (string.IsNullOrEmpty(error))
            {
                CJJCH.Model.T_NET_PicNews model = new Model.T_NET_PicNews();
                model.ID = new Guid(ViewState["ID"].ToString());
                model.HrefUrl = LTP.Common.StringClass.EncodeString(this.txtHrefUrl.Text.Trim());
                model.IsShow = this.chkIsShow.Checked;
                model.OrderNum = Convert.ToInt32(this.txtOrder.Text.Trim());
                model.Instructions = LTP.Common.StringClass.EncodeString(this.txtInstructions.Text.Trim());
                model.BuildDate = DateTime.Now;
                if (this.fileImg.FileName != "")
                {
                    string ImgType = ConfigHelper.GetConfigString("ImgType");
                    int ImgSize = ConfigHelper.GetConfigInt("UploadFileSize");
                    string UploadPic = ConfigHelper.GetConfigString("UploadPic");
                    string fileName = this.lblImg.Text.Trim();
                    error = FileClass.UploadFile(fileImg, ImgType, UploadPic, ImgSize, ref fileName);
                    if (string.IsNullOrEmpty(error))
                    {
                        model.PicUrl = fileName;
                    }
                    else
                    {
                        LTP.Common.WindowClass.WindowBack(error);
                    }
                }
                else
                {
                    model.PicUrl = this.lblImg.Text.Trim();
                }
                error = bll_pic.UpdatePicNews(model);
                if (error == "")
                {
                    Response.Redirect("PicNewsList.aspx");
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }


        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("PicNewsList.aspx");
        }

        private string CheckInput()
        {
            StringBuilder error = new StringBuilder();
            if (txtHrefUrl.Text.Trim() == "")
            {
                error.Append("请输入图片链接地址！\\n");
            }
            if (txtOrder.Text.Trim() == "")
            {
                error.Append("请输入排序号！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(txtOrder.Text.Trim()))
            {
                error.Append("请输入正确的排序号！\\n");
            }
            if (this.txtInstructions.Text.Trim()=="")
            {
                error.Append("请输入新闻说明！ \\n");
            }
            else if(this.txtInstructions.Text.Length >100)
            {
                error.Append("新闻说明最多输入100个字！ \\n");
            }
            return error.ToString();
        }
    }
}